package nimbus.errors
{	
	import flash.events.EventDispatcher;
	
	import nimbus.events.ErrorLogEvent;
	import nimbus.utils.getSingletonByDefinition;
	
	public class ErrorLog extends EventDispatcher
	{
		public static var log:Array = [];
		public static var debugMode:Boolean = true;
		
		public static function getInstance():ErrorLog
		{
			return getSingletonByDefinition(ErrorLog) as ErrorLog;
		}
		
		public static function throwError(error:Error):void
		{
			log.push(error);
			getInstance().dispatchEvent(new ErrorLogEvent(ErrorLogEvent.ERROR, error));
			
			if(debugMode)
				throw error;
		}
		
		public static function testStaticConstructor(classObject:Object):void
		{
			
			throwError(new InvalidConstructorError(classObject));
		}
		
		public static function testSingletonInstantiation(classObject:Object):void
		{
			if( !(classObject is Class))
				classObject = classObject.constructor;
			 
			if(getSingletonByDefinition(classObject as Class) !== true)
				throwError(new InstantiationError(classObject));
		}
		
		public static function testAbstractInstantiation(thisReference:Object, classObject:Object):void
		{
			if( !(classObject is Class))
				classObject = classObject.constructor;
			
			if(thisReference.constructor == classObject)
				throwError(new InstantiationError(classObject));	
		}
		
		public static function testAbstractMethodCall(classObject:Object, methodName:String = null):void
		{
			throwError(new AbstractMethodCallError(classObject, methodName));
		}
		
		public function ErrorLog()
		{
			testSingletonInstantiation(ErrorLog);
		}
		
	}
}
